﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FontConversionForm.cs" company="Voogd en Voogd Diensten">
//   Copyright 2008-2009 Voogd en Voogd Diensten
// </copyright>
// <author>Jelle</author>
// <summary>
//   De FontConversionForm class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace GLCDDesigner2
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Windows.Forms;

    using GLCDBaseTypes.BaseTypes;
    using GLCDBaseTypes.PluginInterfaces;

    public partial class FontConversionForm : Form
    {
        public FontConversionForm()
        {
            InitializeComponent();

            Assembly moduleAssembly = Assembly.LoadFrom(@"C:\Users\Jelle\Documents\Visual Studio 2010\Projects\GLCDDesigner\GLCDST7565Plugin\bin\Debug\GLCDST7565Plugin.dll");

            Type[] types = moduleAssembly.GetTypes();

            foreach (var instance in
                from type in types
                where type.GetInterface("ILcdPlugin") != null
                select (ILcdPlugin)Activator.CreateInstance(type))
            {
                this.toolStripComboBox1.Items.Add(instance);
            }

            this.toolStripComboBox1.SelectedIndex = 0;
        }

        private void fontComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.UpdateFontPreview();

            this.UpdateCodeAndBitmaps();
        }

        private void UpdateCodeAndBitmaps()
        {
            richTextBox2.Text = ((ILcdPlugin)toolStripComboBox1.SelectedItem).GetArduinoCodeForFont(
                richTextBox1.Font, (int)numericUpDown1.Value, (int)numericUpDown1.Value, toolStripTextBox1.Text);

            List<HexRepresentationItem> hexItems2 = ((ILcdPlugin)toolStripComboBox1.SelectedItem).GetHexList(richTextBox1.Font, (int)numericUpDown1.Value, (int)numericUpDown1.Value);

            Bitmap bmp2 = ((ILcdPlugin)toolStripComboBox1.SelectedItem).CreateBitmapFromHex(hexItems2, (int)numericUpDown1.Value, 2);
            pictureBoxLCDVersion.Image = bmp2;
        }

        /// <summary>
        /// Update font preview
        /// </summary>
        private void UpdateFontPreview()
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < 255; i++)
            {
                char c = (char)i;

                // Get display string.
                string display = string.Empty;

                if (char.IsWhiteSpace(c))
                {
                    display = c.ToString();

                    switch (c)
                    {
                        case '\t':
                            display = string.Empty;
                            break;
                        case ' ':
                            display = " ";
                            break;
                        case '\n':
                            display = string.Empty;
                            break;
                        case '\r':
                            display = string.Empty;
                            break;
                        case '\v':
                            display = string.Empty;
                            break;
                        case '\f':
                            display = string.Empty;
                            break;
                    }
                }
                else if (char.IsControl(c))
                {
                    display = string.Empty;
                }
                else
                {
                    display = c.ToString();
                }

                sb.Append(display);
            }

            FontStyle fs = 0;

            if (checkBoxBold.Checked)
            {
                fs = fs | FontStyle.Bold;
            }

            if (checkBoxItalic.Checked)
            {
                fs = fs | FontStyle.Italic;
            }

            if (checkBoxUnderline.Checked)
            {
                fs = fs | FontStyle.Underline;
            }

            Font font = new Font((string)fontComboBox1.SelectedItem, (int)numericUpDown1.Value, fs);

            richTextBox1.Font = font;

            richTextBox1.Text = sb.ToString();
        }
    }
}
